package com.yhp.web;

import com.yhp.bean.Menu;
import com.yhp.service.impl.MenuServiceImpl;
import com.yhp.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/power/menu/menus" )
public class MenuServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      String method = req.getParameter("method");
       if ("select".equals(method)){
           select(req,resp);
       }
    }

    private void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      //模糊查询数据
        String index = req.getParameter("index");
        int pageIndex=(index==null||index.length()==0)?1:Integer.parseInt(index);
      //调取service方法
        PageUtil pageUtil = new PageUtil();
        List<Menu> menuList = new MenuServiceImpl().getMenuList(pageIndex,pageUtil.getPageSize());
        //获取总条数
        MenuServiceImpl menuService = new MenuServiceImpl();
        pageUtil.setTotal(menuService.total());
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDataList(menuList);
        pageUtil.setTotalPages(pageUtil.getTotalPages());
        //跳转页面
        req.setAttribute("mi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }

}
